2025.7.16 3Dグラフ(等高線)【matplotlib】
contour 関数を使う。
code:p.py
import torch as pt
import matplotlib.pyplot as plt
x = pt.linspace(-5, 5, 100)
y = pt.linspace(-5, 5, 100)
xx, yy = pt.meshgrid(x, y, indexing='xy')
zz = (xx**2 + yy - 11)**2 + (xx + yy**2 - 7)**2
levels = 30 # <--- 等高線の細かさ
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
ax.contour(xx, yy, zz, levels) # <--- 細かさを第4引数として与える
plt.show()
https://scrapbox.io/files/68775754700e1d65222a6838.png
等高線は引数 offset で指定した高さにプロットされる。
code:p.py
import torch as pt
import matplotlib.pyplot as plt
x = pt.linspace(-5, 5, 100)
y = pt.linspace(-5, 5, 100)
xx, yy = pt.meshgrid(x, y, indexing='xy')
zz = (xx**2 + yy - 11)**2 + (xx + yy**2 - 7)**2
levels = 30
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
ax.contour(xx, yy, zz, levels, offset=0) # <---(!)
plt.show()
https://scrapbox.io/files/687757eb6497e1395297d28b.png